//
//  RegisterViewController.m
//  GoldOperationClient
//
//  Created by yaxue wang on 16/8/8.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "RegisterViewController.h"
#import "LoginViewController.h"
#import "TPKeyboardAvoidingScrollView.h"
#import "RegisterNextViewController.h"
#import "CTextField.h"
@interface RegisterViewController()
{
    CTextField *_phoneText;
    CTextField *_verText;
}
@property(nonatomic, assign)BOOL hasSendCode;
@property(nonatomic, strong)UIButton * sendBtn;
@property(nonatomic, strong)TPKeyboardAvoidingScrollView * scrollView;


@end
@implementation RegisterViewController
- (void)initView
{
    [super initView];
    [self setTitle:@"注册"];
    [self.navBar createRightBtn:@"已有账号" block:^{
        //登录界面
        LoginViewController *registerVC =[[LoginViewController alloc]init];
        [self pushController:registerVC];
    }];
    [self layoutView];
    
}
- (void)layoutView
{
    _scrollView =[[TPKeyboardAvoidingScrollView alloc]initWithFrame:CGRectMake(0, 64, WindowWidth, WindowHeight-64)];
    _scrollView.backgroundColor =BackColor;
    [self.view addSubview:_scrollView];
    
    for (int i=0; i<2; i++) {
        UILabel*lab=[[UILabel alloc]init];
        lab.frame =CGRectMake(25, 5+(39+48)*i, WindowWidth-25, 39);
        lab.font= F(14);
        lab.textColor=FontColor;
        [_scrollView addSubview:lab];
        NSArray *ar =[[NSArray alloc]initWithObjects:@"手机号码",@"验证码",nil];
        lab.text =[ar objectAtIndex:i];
        UIView *bgView =[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(lab.frame), WindowWidth, 48)];
        bgView.backgroundColor =[UIColor whiteColor];
        [_scrollView addSubview:bgView];
        UILabel*line=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 0.5)];
        line.backgroundColor =LineColor;
        [bgView addSubview:line];
        UILabel*line1=[[UILabel alloc]initWithFrame:CGRectMake(0, 47.5, WindowWidth, 0.5)];
        line1.backgroundColor =LineColor;
        [bgView addSubview:line1];
        if (i==0) {
            
            _phoneText =[[CTextField alloc]initWithFrame:CGRectMake(25, 0.5, WindowWidth-50, 47)];
            _phoneText.placeholder =@"请输入手机号码";
            [_phoneText setNullMsg:@"请输入11位手机号!"];
            _phoneText.font =F(12);
            [_phoneText setInputType:CTextFieldInputTypePhone];
            _phoneText.tag =100;
            [bgView addSubview:_phoneText];
        }else
        {
            _verText =[[CTextField alloc]initWithFrame:CGRectMake(25, 0.5, WindowWidth-130, 47)];
            _verText.placeholder =@"请输入验证码";
            _verText.font =F(12);
            _verText.inputType =CTextFieldInputTypeNum;
            [_verText setMaxLength:6 LengthChangeBlock:nil];
            _verText.tag =101;
            [bgView addSubview:_verText];
            self.sendBtn =[UIButton buttonWithType:UIButtonTypeCustom];
            self.sendBtn.frame =CGRectMake(WindowWidth-106, 7.5, 86, 33);
            [self.sendBtn setBackgroundColor:SendColor];
            self.sendBtn.layer.masksToBounds=YES;
            self.sendBtn.layer.cornerRadius=4;
            self.sendBtn.userInteractionEnabled =YES;
            [self.sendBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
            self.sendBtn.titleLabel.font =[UIFont systemFontOfSize:14];
            [bgView addSubview:self.sendBtn];
            [self.sendBtn bk_addEventHandler:^(id sender) {
                
                
                if (!_phoneText.verfiy) {
                    
                    return;
                }
                //判断注册没有注册
                [CClass HttpRequestTask:YES withUrl:@"app_registryCheckCaptcha" withData:@{@"mobile":_phoneText.value,@"isCheckRegistry":@"0",@"customerType":@"02",@"sendIfRegistered":@"1"} notifyError:YES onSuccess:^(BaseModel *model) {
                    //成功的时候
                    
                    _hasSendCode = true;
                    
                    [self.sendBtn setEnabled:NO];
                    __block NSInteger t = 60;
                    NSTimeInterval period = 1.0; //设置时间间隔
                    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
                    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
                    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0); //每秒执行
                    dispatch_source_set_event_handler(_timer, ^{
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [self.sendBtn setTitle:[NSString stringWithFormat:@"%zds", t] forState:UIControlStateNormal];
                            [self.sendBtn setBackgroundColor:[UIColor colorFromHexString:@"b3babd"]];
                            
                        });
                        if (t -- <= 0 ) {
                            dispatch_source_cancel(_timer);
                            dispatch_async(dispatch_get_main_queue(), ^{
                                [self.sendBtn setEnabled:YES];
                                [self.sendBtn setTitle:@"重发验证码" forState:UIControlStateNormal];
                                [self.sendBtn setBackgroundColor:SendColor];
                            });
                        }
                    });
                    dispatch_resume(_timer);
                    
                    
                    
                } onError:^(BaseModel *model) {
                    
                    //出错时候
                }];
                
                
                
            } forControlEvents:UIControlEventTouchUpInside];
            
        }
        
    }
    
    
    UIButton *loginBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    loginBtn.frame =CGRectMake(40, 179+40, WindowWidth-80, 35);
    [loginBtn setBackgroundColor:MainColor];
    loginBtn.layer.masksToBounds=YES;
    loginBtn.layer.cornerRadius=8;
    [loginBtn setTitle:@"下一步" forState:UIControlStateNormal];
    loginBtn.titleLabel.font =F(15);
    [loginBtn addTarget:self action:@selector(gotoNext:) forControlEvents:UIControlEventTouchUpInside];
    [_scrollView addSubview:loginBtn];

    
    
}

- (void)gotoNext:(UIButton*)sender
{
    
    if (_phoneText.value.length==0) {
        [CClass showMessage:@"请输入手机号!"];
        return;
    }
    if (_hasSendCode == false) {
        [CClass showMessage:@"请先发送验证码!"];
        return ;
    }
    if (_verText.value.length==0) {
        [CClass showMessage:@"请输入验证码!"];
        return;
    }
    
    [self setIsLoading:YES];
    [CClass HttpRequestTask:YES withUrl:@"app_registryCheckMobile" withData:@{@"captcha":_verText.text,@"mobile":_phoneText.value,@"customerType":@"02"} notifyError:YES onSuccess:^(BaseModel *model) {
        [self setIsLoading:NO];
    
        RegisterNextViewController *registerNextVC =[[RegisterNextViewController alloc]init];
        registerNextVC.phone =_phoneText.value;
        [self pushController:registerNextVC];
    } onError:^(BaseModel *model) {
        [CClass showMessage:model.B_msg];
    }];

}
@end
